package com.alennus.blog.service;

import com.alennus.blog.domain.Menu;
import com.alennus.blog.domain.RoleMenu;
import com.alennus.blog.repository.MenuRepository;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Created by pactera on 2018/5/18.
 */
@Service
public class MenuService {


    @Autowired
    MenuRepository menuRepository;

    @Autowired
    MongoTemplate template;


    public Menu save(Menu menu){

        return menuRepository.save(menu);
    }

    public Menu findById(String id){
        return menuRepository.findById(id).get();
    }

    public List<Menu> findByParentId(String id){
        return menuRepository.findByParentId(id);
    }

    public Page<Menu> findMenu(Menu menu, Pageable pageable){
        Criteria criteria = new Criteria();
        if(!StringUtils.isEmpty(menu.getParentId())){
            criteria.and("parentId").is(menu.getParentId());
        }
        Query query = new Query(criteria);
        //查询
        List<Menu> list = template.find(query.with(pageable), Menu.class);
        list = addParentName(list);
        Long count =  template.count(query, Menu.class);
        //将集合与分页结果封装
        return new PageImpl<>(list, pageable, count);
    }


    private List<Menu> menus(Menu menu){
        Criteria criteria = new Criteria();
        if(!StringUtils.isEmpty(menu.getParentId())){
            criteria.and("parentId").is(menu.getParentId());
        }
        Query query = new Query(criteria);
        //查询
        List<Menu> list = template.find(query, Menu.class);
        return addParentName(list);
    }

    private List<Menu> addParentName(List<Menu> list){
        for(Menu menu1 : list){
            String parentId = menu1.getParentId();
            if(!StringUtils.isEmpty(parentId) && !parentId.equals("-1")){
                Menu menu2 = findById(parentId);
                menu1.setParentName(menu2.getTitle());
            }else{
                menu1.setParentName("菜单");
            }
        }
        return list;
    }

    public List<Menu> getMenuTree(){
        Menu menu = new Menu();
        menu.setParentId("-1");
        List<Menu> list = menus(menu);
        for(Menu menu1 : list){
            menu.setParentId(menu1.getId());
            menu1.setChildren(menus(menu));
        }
        return list;

    }

    public Boolean deleteAll(List<Menu> menus){
        menuRepository.deleteAll(menus);
        return Boolean.TRUE;
    }

    public List<Menu> menuTree(String parentId,List<String> checkedMenu){
        List<Menu> menus = getMenusByParentId(parentId);
        for(Menu menu1 : menus){
            List<Menu> m = menuTree2(menu1.getId(),checkedMenu);
            menu1.getChildren().addAll(m);
            menu1.setExpand(Boolean.TRUE);
            if(menu1.getChildren() == null || menu1.getChildren().size() < 1){
                menu1.setExpand(Boolean.FALSE);
            }
            if(checkedMenu.contains(menu1.getId())){
                menu1.setChecked(Boolean.TRUE);
            }
        }

        return menus;
    }

    public List<Menu> menuTree2(String parentId,List<String> checkedMenu){
        List<Menu> menus = getMenusByParentId(parentId);
        for(Menu menu1 : menus){
            List<Menu> m = menuTree(menu1.getId(),checkedMenu);
            menu1.getChildren().addAll(m);
            menu1.setExpand(Boolean.TRUE);
        }
        return menus;
    }
    private List<Menu> getMenusByParentId(String parentId){
        return menuRepository.findByParentId(parentId);
    }

}
